package cn.wetcland.es.config.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@ControllerAdvice(basePackages = "cn.wetcland.es")
public class WebExceptionHandler {

    /**
     * 将自定义异常捕获，以友好的方式告知用户
     * @param customerException 自定义异常
     * @return AjaxResponse
     */
    @ResponseBody
    @ExceptionHandler(CustomerException.class)
    public ModelAndView customerException(CustomerException customerException, HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("my-error");
        modelAndView.addObject("errMsg", customerException.getMessage());
        return modelAndView;
    }

}
